package com.fpt.edu.vn.h2tv.service;

import java.util.ArrayList;
import java.util.List;

import com.fpt.edu.vn.h2tv.common.ElementName;
import com.fpt.edu.vn.h2tv.dao.EntityManagerHelper;
import com.fpt.edu.vn.h2tv.dao.ITbCategoryDAO;
import com.fpt.edu.vn.h2tv.dao.TbCategoryDAO;
import com.fpt.edu.vn.h2tv.dao.entity.TbCategory;
import com.fpt.edu.vn.h2tv.dto.Category;
import com.fpt.edu.vn.h2tv.dto.CategoryResponse;

public class CategoryService implements IBaseService {

    @Override
    public Object getResponse() {
        return getCategoryResponse();
    }
    /**
     * Get all category Return to client
     * @return CategoryResponse
     */
    private CategoryResponse getCategoryResponse() {
        ITbCategoryDAO dao = new TbCategoryDAO();
        // find all category on database
        List<TbCategory> tbCategoryList = dao.findAll();
        CategoryResponse response = new CategoryResponse();
        List<Category> categoryList = new ArrayList<Category>();
        if (tbCategoryList != null && tbCategoryList.size() > 0) {
            for (TbCategory category : tbCategoryList) {
                Category categoryDTO = new Category();
                categoryDTO.setCategoryId(category.getCategoryId().intValue());
                categoryDTO.setCategoryName(category.getCategoryName());
                categoryList.add(categoryDTO);
            }
            response.setStatus(ElementName.SUCCESS);
        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setCategoris(categoryList);
        return response;
    }

    public static String addCategory(String cateName) {
        ITbCategoryDAO cateDAO = new TbCategoryDAO();
        TbCategory category = new TbCategory();
        category.setCategoryName(cateName);

        // Transaction
        EntityManagerHelper.beginTransaction();
        cateDAO.save(category);
        EntityManagerHelper.commit();

        String success = "Add new category successful!";
        return success;
    }

    
    public static String updateCategory(Short id, String cateName) {
        ITbCategoryDAO cateDAO = new TbCategoryDAO();
        TbCategory category = cateDAO.findById(id);        
        String success;
        if (category != null) {
            // Transaction
            category.setCategoryName(cateName);
            EntityManagerHelper.beginTransaction();
            cateDAO.update(category);
            EntityManagerHelper.commit();
            success = "update category " + id + " successful!";
        } else {
            success = "update category " + id + " failed!";
        }

        return success;
    }

    public static String deleteCategory(Short id) {
        ITbCategoryDAO cateDAO = new TbCategoryDAO();
        TbCategory category = cateDAO.findById(id);
        String success;
        if (category != null) {
            // Transaction
            EntityManagerHelper.beginTransaction();
            cateDAO.delete(category);
            EntityManagerHelper.commit();
            success = "delete category " + id + " successful!";
        } else {
            success = "delete category " + id + " failed!";
        }

        return success;
    }

    public static CategoryResponse searchCategory(String key) {
        ITbCategoryDAO cateDAO = new TbCategoryDAO();
        List<TbCategory> tbCategoryList = cateDAO.findCategoryByName(key);
        CategoryResponse response = new CategoryResponse();
        List<Category> categoryList = new ArrayList<Category>();
        if (tbCategoryList != null && tbCategoryList.size() > 0) {
            for (TbCategory category : tbCategoryList) {
                Category categoryDTO = new Category();
                categoryDTO.setCategoryId(category.getCategoryId().intValue());
                categoryDTO.setCategoryName(category.getCategoryName());
                categoryList.add(categoryDTO);
            }
            response.setStatus(ElementName.SUCCESS);
        } else {
            response.setStatus(ElementName.NO_DATA);
        }
        response.setCategoris(categoryList);
        return response;
    }

}
